﻿//Write a program to convert hexadecimal numbers to binary numbers (directly).


using System;
using System.Text;

public class HexadecimalToBinary
{
    public static void Main()
    {
        Console.Write("Enter hexadecimal number to convert to binary\nNum = ");
        string number = Console.ReadLine().ToUpper();
        byte[] array = PopulateArray(number);
        string[] str = Patern(array);
        Console.WriteLine("Converted Hexadecimal number is:");
        Print(str);
    }

    private static string[] Patern(byte[] array)
    {
        string[] binary = new string[] { "0000", "0001", "0010", "0011",
                                         "0100", "0101", "0110", "0111",
                                         "1000", "1001", "1010", "1011",
                                         "1100", "1101", "1110", "1111"
                                      };
        string[] hex = new string[] { "0", "1", "2", "3",
                                      "4", "5", "6", "7",
                                      "8", "9", "A", "B",
                                      "C", "D", "E", "F"
                                     };
        string[] result = new string[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = binary[(array[i] - 48 > 9 ? (array[i] - 55) : (array[i] - 48))];
        }

        return result;
    }

    private static byte[] PopulateArray(string str)
    {
        byte[] array = new byte[str.Length];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = (byte)str[i];
        }

        return array;
    }

    public static void Print(string[] array)
    {
        foreach (var item in array)
        {
            Console.Write(item);
        }
        Console.WriteLine();
    }
}